package a66weding.com.jiehuntong.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.MyFragmentAdapter;
import a66weding.com.jiehuntong.fragment.ImageStoryFragment;
import a66weding.com.jiehuntong.fragment.VideoStoryFragment;
import a66weding.com.jiehuntong.view.ViewPagerForWriteStory;

/**
 * 谱写故事界面
 */
public class WriteStoryActivity extends FragmentActivity implements OnClickListener {
    private TextView tv_image_story;//图片故事排序按钮
    private TextView tv_video_story;//视频故事切换按钮
    public   TextView order;//排序按钮
    private ImageStoryFragment imageStoryFragment;//图片故事界面
    private VideoStoryFragment videoStoryFragment;//视频故事界面
    private ArrayList<Fragment> fragmentrs;//视频故事界面,图片故事界面集合
    private ViewPagerForWriteStory mViewPager;
    private MyFragmentAdapter myFragmentAdapter;
    private ArrayList<TextView> buttons;//这里是上面切换图片故事与视频故事的控件集合
    public boolean isEdit = false;//是否可以编辑排序如果数据少于2则不行做个提醒

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_story);
        initView();//初始化控件
        imageStoryFragment = new ImageStoryFragment();
        videoStoryFragment = new VideoStoryFragment();
        fragmentrs = new ArrayList<>();
        buttons = new ArrayList<TextView>();
        buttons.add(tv_image_story);
        buttons.add(tv_video_story);
        fragmentrs.add(imageStoryFragment);
        fragmentrs.add(videoStoryFragment);
        myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), fragmentrs);
        mViewPager.setAdapter(myFragmentAdapter);
        mViewPager.setCurrentItem(0);//默认界面设置
        //设置滑动监听切换fragemt并切换按钮以及设置视频故事排序按钮不显示装态
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    order.setVisibility(View.GONE);
                } else {
                    order.setVisibility(View.VISIBLE);
                }
                for (int i = 0; i < buttons.size(); i++) {
                    if (i == position) {
                        buttons.get(i).setSelected(true);
                    } else {
                        buttons.get(i).setSelected(false);
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
    private void initView() {
        ImageButton imgbtn_back = (ImageButton) findViewById(R.id.imgbtn_back);//返回按钮
        imgbtn_back.setOnClickListener(this);
        tv_image_story = (TextView) findViewById(R.id.tv_image_story);
        tv_video_story = (TextView) findViewById(R.id.tv_video_story);
        mViewPager = (ViewPagerForWriteStory) findViewById(R.id.frl_contain);
        order = (TextView) findViewById(R.id.order);
        tv_image_story.setSelected(true);//设置图片故事按钮是默认选中状态
        tv_video_story.setOnClickListener(this);
        tv_image_story.setOnClickListener(this);
        order.setOnClickListener(this);
    }
    @Override
            public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgbtn_back://返回按钮
                finish();
                break;
            case R.id.tv_image_story://图片故事切换
                mViewPager.setCurrentItem(0);
                break;
            case R.id.tv_video_story://视频故事切换
                mViewPager.setCurrentItem(1);
                break;
            case R.id.order://排序
                if (mViewPager.getCurrentItem() == 0) {
                    ImageStoryFragment fragment = (ImageStoryFragment) fragmentrs.get(0);
                    if (isEdit) {
                        isEdit = false;
                        order.setText("排序");
                        fragment.order();
                    } else {
                        order.setText("完成");
                        isEdit = true;
                        fragment.order();
                    }
                }
                break;
        }
    }

}

